package com.xtkj.superclub_friendscircle.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xtkj.commons.JsonResult;
import com.xtkj.commons.SnowflakeIdWorker;
import com.xtkj.superclub_entity.entity.friendsCircle.FriendCicleShare;
import com.xtkj.superclub_entity.entity.friendsCircle.ShareComment;
import com.xtkj.superclub_entity.entity.friendsCircle.ShareReply;
import com.xtkj.superclub_friendscircle.dao.*;
import com.xtkj.superclub_friendscircle.service.IFriendsCircleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;

/**
 * 项目名称：superclub
 * 类 名 称：FriendsCircleServiceImpl
 * 类 描 述：TODO
 * 创建时间：2020/1/2 16:25
 * 创 建 人：xiezhiyan
 */
@Service
public class FriendsCircleServiceImpl implements IFriendsCircleService {

    @Autowired
    private IFriendCicleShareDao friendCicleShareDao;
    @Autowired
    private IShareReplyDao shareReplyDao;
    @Autowired
    private IShareCommentDao shareCommentDao;
    @Autowired
    private SnowflakeIdWorker snowflakeIdWorker;

    /**
     * @return com.xtkj.commons.JsonResult
     * @description 方法描述
     * 获取所有用户发布的朋友圈信息
     * @author xiezhiyan
     * @date 2020/1/2 16:29
     */
    @Override
    public JsonResult allFriendsCircle(Integer page, Integer size) {
        // TODO: 2020/1/2 查询分享表中时间在新的30条记录,记录包含的内容包括（分享者id,简要描述，分享标题，内容图片，活动开始时间，分享id,分享类型）
        QueryWrapper<FriendCicleShare> wrapper = new QueryWrapper<FriendCicleShare>().orderByDesc("updatedtm");
        Page<FriendCicleShare> friendCicleSharePage = friendCicleShareDao.selectPage(new Page<>(page, size), wrapper);
        List<FriendCicleShare> shareList = friendCicleSharePage.getRecords();
        for (FriendCicleShare friendCicleShare : shareList) {
            String shareId = friendCicleShare.getShareId();
            //查询所有的评论
            QueryWrapper<ShareComment> wrapperComment = new QueryWrapper<ShareComment>().eq("share_id", shareId);
            List<ShareComment> shareComments = shareCommentDao.selectList(wrapperComment);
            for (ShareComment shareComment : shareComments) {
                //查询所有的回复
                String commitId = shareComment.getCommentId();
                QueryWrapper<ShareReply> wrapperReply = new QueryWrapper<ShareReply>().eq("comment_id", commitId);
                List<ShareReply> shareReplies = shareReplyDao.selectList(wrapperReply);
                shareComment.setShareReply(shareReplies);
            }
            friendCicleShare.setShareCommit(shareComments);
        }
        if (CollectionUtils.isEmpty(shareList)) {
            return JsonResult.error().setCode(500).setMsg("查询错误");
        } else {
            return JsonResult.success().setCode(200).setMsg("查询成功").setData(shareList);
        }
    }

    /**
     * @description 方法描述
     * 发送朋友圈
     * @param friendCicleShare
     * @return void
     * @author xiezhiyan
     * @date 2020/1/6 18:49
     */
    @Override
    public void sendFriendsInfo(FriendCicleShare friendCicleShare) {
        friendCicleShare.setShareId(snowflakeIdWorker.nextId()+"").setUpdatedtm(new Date()).setUpdatedtm(new Date());
        friendCicleShareDao.insert(friendCicleShare);
    }

}
